﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class TaiKhoanDAO
    {
        // Operation...
        public static bool ThemTaiKhoan(TaiKhoanDTO taikhoan)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Insert into TAIKHOAN(Username, [Password], HoTen, LoaiTaiKhoan) values(@Username, @Password, @HoTen, @LoaiTaiKhoan)";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@Username", OleDbType.VarChar);
                thamso.Value = taikhoan.Username;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Password", OleDbType.VarChar);
                thamso.Value = taikhoan.Password;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@HoTen", OleDbType.VarChar);
                thamso.Value = taikhoan.HoTen;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@LoaiTaiKhoan", OleDbType.VarChar);
                thamso.Value = taikhoan.LoaiTaiKhoan;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool XoaTaiKhoan(string username)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Delete From TAIKHOAN Where Username = @Username";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso = new OleDbParameter("@Username", OleDbType.VarChar);
                thamso.Value = username;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool SuaThongTinTaiKhoan(TaiKhoanDTO taikhoan)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Update TAIKHOAN Set Username = @Username, [Password] = @Password, HoTen = @HoTen, LoaiTaiKhoan = @LoaiTaiKhoan Where MaTaiKhoan = @MaTaiKhoan";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso;

                thamso = new OleDbParameter("@Username", OleDbType.VarChar);
                thamso.Value = taikhoan.Username;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@Password", OleDbType.VarChar);
                thamso.Value = taikhoan.Password;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@HoTen", OleDbType.VarChar);
                thamso.Value = taikhoan.HoTen;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@LoaiTaiKhoan", OleDbType.VarChar);
                thamso.Value = taikhoan.LoaiTaiKhoan;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@MaTaiKhoan", OleDbType.Integer);
                thamso.Value = taikhoan.MaTaiKhoan;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static List<TaiKhoanDTO> LayDanhSachTaiKhoan()
        {
            List<TaiKhoanDTO> dsTaiKhoan = new List<TaiKhoanDTO>();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select Username, LoaiTaiKhoan From TAIKHOAN";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    TaiKhoanDTO taikhoan = new TaiKhoanDTO();
                    taikhoan.Username= bodoc.GetString(0);
                    if (!bodoc.IsDBNull(1))
                        taikhoan.LoaiTaiKhoan = bodoc.GetString(1);

                    dsTaiKhoan.Add(taikhoan);
                }
            }
            catch (Exception ex)
            {
                dsTaiKhoan = new List<TaiKhoanDTO>();
            }

            return dsTaiKhoan;
        }

        public static TaiKhoanDTO LayThongTinTaiKhoan(String username)
        {
            TaiKhoanDTO taikhoan = new TaiKhoanDTO();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select * From TAIKHOAN Where Username = @Username";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);

                OleDbParameter thamso = new OleDbParameter("@Username", OleDbType.VarChar);
                thamso.Value = username;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    taikhoan.MaTaiKhoan = bodoc.GetInt32(0);
                    if (!bodoc.IsDBNull(1))
                        taikhoan.Username = bodoc.GetString(1);
                    if (!bodoc.IsDBNull(2))
                        taikhoan.Password = bodoc.GetString(2);
                    if (!bodoc.IsDBNull(3))
                        taikhoan.HoTen = bodoc.GetString(3);
                    if (!bodoc.IsDBNull(4))
                        taikhoan.LoaiTaiKhoan = bodoc.GetString(4);
                }
            }
            catch (Exception ex)
            {
                taikhoan = new TaiKhoanDTO();
            }

            return taikhoan;
        }
    }
}
